// pages/OrderList/OrderList.js
import {get, post} from '../../utils/request'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    active: 0
  },
  onClick(e) {
    const type = e.detail.index
    this.getOrderList(type)
    // if()
  },
  //确认收货
  confirmGoods({currentTarget:{dataset:{index}}}) {
    const orderId = this.data.orderList[index] //获取自定义属性里面的index索引
    post({ //发送确认收货请求
      url: '/order/confirm',
      data: {
        orderId 
      }
    }).then(res => {
      // console.log(res);
      if(res.data.errno === 0) { //如果errno为0说明请求成功
        Toast.loading({ 
          message: '正在确认中...',
          forbidClick: true,
          duration: 800
        });
        setTimeout(() => {
          Toast.success('收货成功');
          this.data.orderList.splice(index,1) //手动删除当前项
          this.setData({ //重新设置数组
            orderList: this.data.orderList
          })
        },1300)
      }
    })
  },
  //立即支付
  atOncePay({currentTarget:{dataset:{index}}}) {
    const orderId = this.data.orderList[index].id
    post({
      url: '/order/pay',
      data: {
        orderId  
      }
    }).then(res => {
      if(res.data.errno === 0) {
        Toast.loading({
          message: '正在支付中...',
          forbidClick: true,
          duration: 800
        });
        setTimeout(() => {
          Toast.success('支付成功');
          this.data.orderList.splice(index,1)
          this.setData({
            orderList: this.data.orderList
          })
        },1300)
      }
    })
  },
  //取消订单
  cancelOrder({currentTarget:{dataset:{index}}}) {
    const orderId = this.data.orderList[index].id
    Dialog.confirm({
      title: '确定要取消订单吗？',
    })
      .then(() => {
        // on confirm
        post({
          url: '/order/cancel',
          data: {
            orderId
          }
        }).then(res => {
          // console.log(res);
          if(res.data.errno === 0) {
            Toast.loading({
              message: '正在取消中...',
              forbidClick: true,
              duration: 800
            });
            setTimeout(() => {
              Toast.success('取消成功');
              this.data.orderList.splice(index,1)
              this.setData({
                orderList: this.data.orderList
              })
            },1300)
          }
        })
      })
      .catch(() => {
        // on cancel
      });
   
  },
  //发起退款
  refund({currentTarget:{dataset:{index}}}) {
    const orderId = this.data.orderList[index].id

    Dialog.confirm({
      title: '确定要取消订单并退款吗？',
    })
      .then(() => {
        // on confirm
        post({
          url: '/order/refund',
          data: {
            orderId
          }
        }).then(res => {
          // console.log(res);
          if(res.data.errno === 0) {
            Toast.loading({
              message: '正在取消中...',
              forbidClick: true,
              duration: 800
            });
            setTimeout(() => {
              Toast.success('取消成功,请稍等退款到账');
              this.data.orderList.splice(index,1)
              this.setData({
                orderList: this.data.orderList
              })
            },1300)
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  //获取订单列表
  getOrderList(showType = 0) {
    // console.log(type);
    get({
      url: '/order/list',
      data: {
        showType
      }
    }).then( ({data:{data}}) => {
      this.setData({
        orderList: data.list
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      active: app.globalData.orderIndex
    })
    this.getOrderList(3)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})